﻿using Autofac;
using System.Reflection;
using System;
using System.IO;
using System.Threading.Tasks;
using System.Collections.Generic;
using Rw.Core.Common.Helper;

namespace WH.Extensions.Setup.AutofacSetup
{
    public class AutofacModuleRegister : Autofac.Module
    {
        protected override void Load(ContainerBuilder builder)
        {

            var basePath = AppDomain.CurrentDomain.BaseDirectory;

            var servicesDllFile = Path.Combine(basePath, "WH.Services.dll");
            var repositoryDllFile = Path.Combine(basePath, "WH.Repositories.dll");

            if (!(File.Exists(servicesDllFile) && File.Exists(repositoryDllFile)))
            {
                var msg = "Repository.dll和service.dll 丢失，因为项目解耦了，所以需要先F6编译，再F5运行，请检查 bin 文件夹，并拷贝。";
                
                throw new Exception(msg);
            }

            var assemblysServices = Assembly.LoadFrom(servicesDllFile);
            builder.RegisterAssemblyTypes(assemblysServices)
                      .AsImplementedInterfaces()
                      .InstancePerDependency();


            var assemblysRepository = Assembly.LoadFrom(repositoryDllFile);
            builder.RegisterAssemblyTypes(assemblysRepository)
                   .AsImplementedInterfaces()
                   .InstancePerDependency();
        }
    }
}
